<?php

require_once APP_PATH.'module/order/library/order_exl_abstract.class.php';		
class get_exl extends order_exl_abstract {
   public function _get_exl($rowVal,$data,$supplier_name,$status){
   	require_once APP_PATH.'module/order/library/phpExcel/phpexcel.php';
	require_once APP_PATH.'module/order/library/phpExcel/PHPExcel/IOFactory.php';
	$count = count($data);
	$objPHPExcel = new phpexcel();
	$objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
								 ->setLastModifiedBy("Maarten Balliauw")
								 ->setTitle("Office 2007 XLSX Test Document")
								 ->setSubject("Office 2007 XLSX Test Document")
								 ->setDescription("Document for Office 2007 XLSX, generated using PHP classes.")
								 ->setKeywords("office 2007 openxml php")
								 ->setCategory("Test result file");							 
	$objPHPExcel->setActiveSheetIndex(0);
	
	//Set document security 设置文档安全
	$objPHPExcel->getSecurity()->setLockWindows(false);
	$objPHPExcel->getSecurity()->setLockStructure(false);
	//$objPHPExcel->getSecurity()->setWorkbookPassword("PHPExcel");//审阅密码
		
	$objRichText = new PHPExcel_RichText();
	$objRichText->createText('');
	$objPayable = $objRichText->createTextRun('今日订单明细表(导出时间：'.date("Y年m月d日 H:i:s").')');
	$objPHPExcel->getActiveSheet()->getCell('A1')->setValue($objRichText);
	$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);//加粗
	$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(14);//字体大小
	$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_DARKRED);// 文本颜色
	$objPHPExcel->getActiveSheet()->getStyle('A1:O1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
	$objPHPExcel->getActiveSheet()->getStyle('A1:O1')->getFill()->getStartColor()->setARGB('00FFFFE3');	// 底纹
	$objPHPExcel->getActiveSheet()->getStyle('A1:O1')->getBorders()->getBottom()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);//边框宽度
	$objPHPExcel->getActiveSheet()->getStyle('A1:O1')->getBorders()->getBottom()->getColor()->setARGB('FFCCCCCC');//边框颜色

	//设置默认行高
	$objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(18);

	//隐藏A列
	//$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setVisible(false);	
		
	//设置窗口冻结点
	$objPHPExcel->getActiveSheet()->freezePane('B3');		
			
	//初始化行高
	for($i = 1; $i <= $count+2; $i++)
	{	
		
		$br_cell = $data[($i-1)]['br_cell'];
		$j = $i + 2;
		//设置单元格自动换行，行高
		if ($br_cell > 1) {			
			$row_height = 17*$br_cell;
			$objPHPExcel->getActiveSheet()->getRowDimension($j)->setRowHeight($row_height);						
			$objPHPExcel->getActiveSheet()->getStyle('B'.$j)->getAlignment()->setWrapText(true);
			$objPHPExcel->getActiveSheet()->getStyle('A'.$j.':O'.$j)->getFill()->getStartColor()->setARGB('00FFFLDC');// 底纹
			$objPHPExcel->getActiveSheet()->getStyle('A'.$j.':O'.$j)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);					
		}
		$objPHPExcel->getActiveSheet()->getStyle('J'.$j)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
	}

		  //自动调整行高
	$objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(-1);	
			
			
	// 列宽
	//$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setAutoSize(true);
	$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
	$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(22);
	$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(13);
	$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(24);
	$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(12);
	$objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(80);
	$objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(15);
	$objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(45);
	$objPHPExcel->getActiveSheet()->getColumnDimension('I')->setWidth(8);
	$objPHPExcel->getActiveSheet()->getColumnDimension('J')->setWidth(8);
	$objPHPExcel->getActiveSheet()->getColumnDimension('K')->setWidth(8);
	$objPHPExcel->getActiveSheet()->getColumnDimension('L')->setWidth(12);
	$objPHPExcel->getActiveSheet()->getColumnDimension('M')->setWidth(20);
	$objPHPExcel->getActiveSheet()->getColumnDimension('N')->setWidth(20);
	$objPHPExcel->getActiveSheet()->getColumnDimension('O')->setWidth(12);
	$objPHPExcel->getActiveSheet()->getColumnDimension('P')->setWidth(12);
	$objPHPExcel->getActiveSheet()->getColumnDimension('Q')->setWidth(12);

	
	//设置水平与垂直对齐方式
	$objPHPExcel->getActiveSheet()->getStyle('A2:O2')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
	$objPHPExcel->getActiveSheet()->getStyle('A2:O2')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
	//设置列数据类型
	//$objPHPExcel->getActiveSheet()->getStyle('A')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_NUMBER);
	
	//保护单元格    
	//$objExcel->getSheet(0)->getProtection()->setSheet(true);    
	//$objExcel->getSheet(0)->protectCells('A1:C22', 'PHPExcel');
	
	//单元格换行标记符
	//change_line_in_excel_cell = '<br style="mso-data-placement:same-cell;" />';
	
	//显式指定内容类型  
	//$objActSheet->setCellValueExplicit('A5', '847475847857487584',PHPExcel_Cell_DataType::TYPE_STRING);    
	
	//第2行列名
	if ($rowVal){
		$j = 0;
		$DataVal = array();
		foreach($rowVal as $k=>$r){
		     $objPHPExcel->getActiveSheet()->getStyleByColumnAndRow($j, 2)->getFont()->setBold(true);//列名加粗
		     $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($j, 2, $r);
		     $DataVal[] = $k;
		     $j++;
		}
	}
	//格数据处理
	if ($DataVal){
		$array = array('待发货'=>0,'部分发货'=>1,'已发货'=>2);
		$pay = array('支付宝'=>'alipay','银行卡支付'=>'bank','微信支付'=>'wechat_qr');
		$sta = array(''=>'','已支付'=>0,'待付款'=>1,'待发货'=>2,'待收货'=>3,'已完成'=>4,'已取消'=>5,'前台已删除'=>6);
		$j =3;
         // print_r($data);die;  		
		foreach ($data as $k => $v) {   
	$objPHPExcel->getActiveSheet()->setCellValue("A".$j,date('Y-m-d-H:i:s',$v['time']))->setCellValue("B".$j,"'$v[sub_sn]'")->setCellValue("C".$j,array_search($v['method'],$pay))->setCellValue("D".$j,"'$v[trade_no]'")->setCellValue("E".$j,$v['address_name'])->setCellValue("F".$j,$v['address_detail'])->setCellValue("G".$j,$v['address_mobile'])->setCellValue("H".$j,$v['sku_name'].'     '.(empty($v['_sku_spec']) ? ' ':'['.$v['_sku_spec'].']'))
	->setCellValue("I".$j,$v['sku_price'])->setCellValue("J".$j,$v['buy_nums'])->setCellValue("K".$j,$v['money'])->setCellValue("L".$j,$v['real_price'])->setCellValue("M".$j,$v['delivery_amount'])->setCellValue("N".$j,$v['supplier_name'])->setCellValue("O".$j,$v['sku_barcode'])->setCellValue("P".$j,array_search($status,$sta))->setCellValue("Q".$j,array_search($v['delivery_status'],$array));
				$j++;	            	
	       
        }
		
	}
	
    $filename = 'order'.date("Y-m-d His").'.xls';
	$objPHPExcel->setActiveSheetIndex(0);
	$objPHPExcel->getDefaultStyle()->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);//数据居中
	$objPHPExcel->getActiveSheet()->setTitle("$supplier_name");//设置当前工作表名
	$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');//导出文件格式
	//保存在指定位置
	// $objWriter->save(APP_PATH.'excel/'.$filename.".xls");
 //    echo '<script>alert("导出成功");</script>';

    // 输出到浏览器
	header('Pragma:public');	
    header('Content-Type:application/x-msexecl;name="xxxj.xls"');
    header('Content-Disposition: attachment;filename="'.$filename.'"');
    $objWriter->save("php://output"); 

   }  
}
